<?php
namespace App\Listeners;

use Illuminate\Database\Events\QueryExecuted;

/**
 * 监听sql
 * @package App\Listeners
 */
class SqlQueryListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     * @param QueryExecuted $event
     * @return void
     */
    public function handle(QueryExecuted $event)
    {
        if (!defined('IS_WEB') || IS_WEB != 1) {
            return;
        }
        if (env('SQL_LOG') != 0) {
            global $SQL_LOG;
            if (!empty($event->bindings)) {

                $sql = str_replace("?", "'%s'", $event->sql);
                $sql = vsprintf($sql, $event->bindings);
            } else {
                $sql = $event->sql;
            }
            $SQL_LOG[] = ['sql' => $sql, 'connectionName' => $event->connectionName, 'time' => $event->time];
        }
    }
}
